/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.model.postp.PointModel;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_controller.PostpController;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;

/**
 * @author  Penna, S.S. and Pitangueira, Roque
 * @since 24/12/2006
 */
public class VectorResultsElementsDialog extends javax.swing.JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private DesktopPostp desktop;
	private InterfacePostp intFrame;
	private PostpModel postpModel;
	private PostpViewState state;
	
	// Variables declaration - do not modify
	private JButton copyButton1;
	private JButton copyButton2;
	private JButton resetButton;
	private JButton viewButton;
	private JButton closeButton;
	private JButton addButton;
	private JCheckBox openInANewWindowCheckBox;
	private JCheckBox onGaussCheckBox ;
	private JCheckBox onFullMeshCheckBox;
	private JCheckBox onMeshCheckBox;
	private JLabel jLabel1;
	private JList keyValuesList;
	private JList vectorValuesList;
	private JPanel vectorPanel;
	private JPanel jPanel2;
	private JPanel vectorValuesListPanel;
	private JScrollPane keyValuesScrollPane;
	private JScrollPane jScrollPane2;
	private JTextField copyTextField1;
	private JTextField copyTextField2;
	private JTextField vectoV1TextField;
	private JTextField vectoV2TextField;
	private JTextField labelTextField;
	// End of variables declaration
	
	private String[] keysValues;
	private Vector<String> vectorDataValues;

	private VectorResultsElementsDialog thisDlg;
	
	/** Creates new form NewJFrame */
	public VectorResultsElementsDialog() {
		initComponents();
	}
	
	/** Creates new form NewJFrame */
	public VectorResultsElementsDialog(DesktopPostp desktop, ViewState vs, String[] keys) {
		this.desktop = desktop;
		this.intFrame = (InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame();
		this.state = (PostpViewState)vs;
		this.keysValues = new String[keys.length+1];
		for (int i = 0; i < this.keysValues.length-1; i++) {
			this.keysValues[i] = keys[i];
		}
		this.keysValues[keysValues.length-1] = ""; 
		vectorDataValues = new Vector<String>();
		initComponents();
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
	private void initComponents() {
		thisDlg = this;
		this.setTitle("Vector Result"+" - "+this.intFrame.getPostpModel().getLabel());
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		this.setSize(new Dimension(675,345));
		this.setMaximumSize(new Dimension(675,345));
		this.setMinimumSize(new Dimension(675,345));
		this.setPreferredSize(new Dimension(675,345));
		this.setResizable(false);
		this.setAlwaysOnTop(true);
		this.setLocationRelativeTo(this.desktop);
		
		keyValuesScrollPane = new JScrollPane();
		keyValuesList = new JList();
		copyTextField1 = new JTextField();
		copyTextField2 = new JTextField();
		copyButton1 = new JButton();
		copyButton2 = new JButton();
		vectorPanel = new JPanel();
		vectoV1TextField = new JTextField();
		vectoV2TextField = new JTextField();
		jPanel2 = new JPanel();
		jLabel1 = new JLabel();
		labelTextField = new JTextField();
		resetButton = new JButton();
		viewButton = new JButton();
		closeButton = new JButton();
		vectorValuesListPanel = new JPanel();
		jScrollPane2 = new JScrollPane();
		vectorValuesList = new JList();
		addButton = new JButton();
		openInANewWindowCheckBox = new JCheckBox();
		onGaussCheckBox = new JCheckBox();
		onFullMeshCheckBox = new JCheckBox();
		onMeshCheckBox = new JCheckBox();
		
		getContentPane().setLayout(null);
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		keyValuesScrollPane.setBorder(BorderFactory.createTitledBorder(""));
		keyValuesList.setBackground(new Color(0, 0, 0));
		keyValuesList.setFont(new Font("Arial",Font.PLAIN, 12));
		keyValuesList.setForeground(new Color(0, 255, 0));
		keyValuesList.setListData(this.keysValues);
		keyValuesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		keyValuesList.setSelectionBackground(new Color(153, 204, 255));
		keyValuesList.setVisibleRowCount(50);
		keyValuesList.setFixedCellHeight(20);
		keyValuesScrollPane.setViewportView(keyValuesList);
		
		getContentPane().add(keyValuesScrollPane);
		keyValuesScrollPane.setBounds(10, 10, 200, 230);
		
		copyTextField1.setBackground(new Color(0, 0, 0));
		copyTextField1.setEditable(false);
		copyTextField1.setFont(new Font("Arial",Font.PLAIN, 12));
		copyTextField1.setForeground(new Color(0, 255, 0));
		copyTextField1.setHorizontalAlignment(JTextField.CENTER);
		copyTextField1.setText("");
		getContentPane().add(copyTextField1);
		copyTextField1.setBounds(330, 20, 100, 30);
		
		copyTextField2.setBackground(new Color(0, 0, 0));
		copyTextField2.setEditable(false);
		copyTextField2.setFont(new Font("Arial",Font.PLAIN, 12));
		copyTextField2.setForeground(new Color(0, 255, 0));
		copyTextField2.setHorizontalAlignment(JTextField.CENTER);
		copyTextField2.setText("");
		getContentPane().add(copyTextField2);
		copyTextField2.setBounds(330, 60, 100, 30);
		
		copyButton1.setText(">> Copy >>");
		copyButton1.setMaximumSize(new Dimension(95, 23));
		copyButton1.setMinimumSize(new Dimension(95, 23));
		copyButton1.setPreferredSize(new Dimension(95, 23));
		getContentPane().add(copyButton1);
		copyButton1.setBounds(220, 27, 100, 23);
		copyButton1.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						copyTextField1.setText(addText(keyValuesList));
						vectoV1TextField.setText(addText(keyValuesList));
					}
				});
		
		copyButton2.setText(">> Copy >>");
		copyButton2.setMaximumSize(new Dimension(95, 23));
		copyButton2.setMinimumSize(new Dimension(95, 23));
		copyButton2.setPreferredSize(new Dimension(95, 23));
		getContentPane().add(copyButton2);
		copyButton2.setBounds(220, 60, 100, 23);
		copyButton2.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						copyTextField2.setText(addText(keyValuesList));
						vectoV2TextField.setText(addText(keyValuesList));
					}
				});
		
		vectorPanel.setLayout(new GridLayout(2, 2));
		vectorPanel.setBorder(BorderFactory.createTitledBorder("Vector"));
		
		vectoV1TextField.setBackground(new Color(0, 0, 0));
		vectoV1TextField.setEditable(false);
		vectoV1TextField.setFont(new Font("Arial",Font.PLAIN, 12));
		vectoV1TextField.setForeground(new Color(0, 255, 0));
		vectoV1TextField.setHorizontalAlignment(JTextField.CENTER);
		vectoV1TextField.setText("");
		vectorPanel.add(vectoV1TextField);
		
		vectoV2TextField.setBackground(new Color(0, 0, 0));
		vectoV2TextField.setEditable(false);
		vectoV2TextField.setFont(new Font("Arial",Font.PLAIN, 12));
		vectoV2TextField.setForeground(new Color(0, 255, 0));
		vectoV2TextField.setHorizontalAlignment(JTextField.CENTER);
		vectoV2TextField.setText("");
		vectorPanel.add(vectoV2TextField);
		getContentPane().add(vectorPanel);
		vectorPanel.setBounds(220, 100, 90, 130);
		
		jPanel2.setLayout(null);
		
		jPanel2.setBorder(BorderFactory.createTitledBorder("Label"));
		jLabel1.setFont(new Font("Arial",Font.PLAIN, 12));
		jLabel1.setText("Value :");
		jPanel2.add(jLabel1);
		jLabel1.setBounds(10, 30, 60, 30);
		
		labelTextField.setBackground(new Color(0, 0, 0));
		labelTextField.setForeground(new Color(0, 255, 0));
		labelTextField.setCaretColor(new Color(0, 255, 0));
		labelTextField.setText("");
		jPanel2.add(labelTextField);
		labelTextField.setBounds(50, 30, 150, 30);
		
		getContentPane().add(jPanel2);
		jPanel2.setBounds(450, 10, 210, 80);
		
		resetButton.setText("Reset");
		getContentPane().add(resetButton);
		resetButton.setBounds(360, 160, 80, 23);
		
		viewButton.setText("View");
		getContentPane().add(viewButton);
		viewButton.setBounds(360, 200, 80, 23);
		viewButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						if(vectorValuesList.getSelectedValue()!=null){
							view(event);
						}else{
							String message = "Select a value!"; 
							JOptionPane.showMessageDialog(thisDlg, message, "Warning", JOptionPane.INFORMATION_MESSAGE);
						}
						
					}
				});
		
		vectorValuesListPanel.setLayout(new GridLayout());
		
		vectorValuesListPanel.setBorder(BorderFactory.createTitledBorder(""));
		vectorValuesList.setBackground(new Color(0, 0, 0));
		vectorValuesList.setForeground(new Color(0, 255, 0));
		jScrollPane2.setViewportView(vectorValuesList);
		
		vectorValuesListPanel.add(jScrollPane2);
		
		getContentPane().add(vectorValuesListPanel);
		vectorValuesListPanel.setBounds(450, 100, 210, 140);
		
		addButton.setText("Add");
		getContentPane().add(addButton);
		addButton.setBounds(360, 120, 80, 23);
		addButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						if(!vectoV1TextField.getText().equals("")
								||!vectoV2TextField.getText().equals("")){
							addValues();
						}else{
							String message = "You must fill the values!"; 
							JOptionPane.showMessageDialog(thisDlg, message, "Warning", JOptionPane.INFORMATION_MESSAGE);
						}
					}
				});
		
		closeButton.setText("Close");
		getContentPane().add(closeButton);
		closeButton.setBounds(540, 275, 100, 23);
		closeButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						close(event);
					}
				});
		
		openInANewWindowCheckBox.setText("Open in a new Window");
		openInANewWindowCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		openInANewWindowCheckBox.setMargin(new Insets(0, 0, 0, 0));
		getContentPane().add(openInANewWindowCheckBox);
		openInANewWindowCheckBox.setBounds(300, 275, 150, 23);
		
		onGaussCheckBox = new JCheckBox("From Gauss values");
		onGaussCheckBox.setSelected(true);
		ActionListener onGaussListener = new ChoiceListenerOnGaussMesh();       
		onGaussCheckBox.addActionListener(onGaussListener);
		getContentPane().add(onGaussCheckBox);
		onGaussCheckBox.setBounds(20, 240, 200, 20);
		
		onFullMeshCheckBox = new JCheckBox("From Gauss and nodal values");
		onFullMeshCheckBox.setSelected(false);
		ActionListener onFullMeshListener = new ChoiceListenerOnFullMesh();       
		onFullMeshCheckBox.addActionListener(onFullMeshListener);
		getContentPane().add(onFullMeshCheckBox);
		onFullMeshCheckBox.setBounds(20, 265, 200, 20);
		
		onMeshCheckBox = new JCheckBox("From nodal values");
		onMeshCheckBox.setSelected(false);
		ActionListener onMeshListener = new ChoiceListenerOnMesh();       
		onMeshCheckBox.addActionListener(onMeshListener);
		getContentPane().add(onMeshCheckBox);
		onMeshCheckBox.setBounds(20, 290, 200, 20);
		
		pack();
		
	}// </editor-fold>
	
	public void close(ActionEvent event){
		this.dispose();
	}
	
	private void addValues(){
		PostpModel postp = ((InterfacePostp)this.intFrame).getPostpModel();
		for(int i=0;i<postp.getStepList().size();i++){
			StepPostp step = postp.getStepList().get(i);
			ArrayList<PointModel> points = step.getGeoPostpModelList().get(1).getPoints();
			for(int j=0;j<points.size();j++){
				PointModel point  = points.get(j);
				double s1 = (Double)point.getValue(vectoV1TextField.getText());
				double s2 = (Double)point.getValue(vectoV2TextField.getText());
				double v1 = Math.sqrt( Math.pow(s1,2)+ Math.pow(s2,2));
				if(labelTextField.getText().equals("")){
					point.putValue("VectorValue",v1);
					if(!vectorDataValues.contains("VectorValue")){
						vectorDataValues.add("VectorValue");
					}
				}else{
					point.putValue(labelTextField.getText(),v1);
					if(!vectorDataValues.contains(labelTextField.getText())){
						vectorDataValues.add(labelTextField.getText());
					}
				}
				
			}
			
			points = step.getGeoPostpModelList().get(0).getPoints();
			for(int j=0;j<points.size();j++){
				PointModel point  = points.get(j);
				double s1 = (Double)point.getValue(vectoV1TextField.getText());
				double s2 = (Double)point.getValue(vectoV2TextField.getText());
				double v1 = Math.sqrt( Math.pow(s1,2)+ Math.pow(s2,2));
				if(labelTextField.getText().equals("")){
					point.putValue("VectorValue",v1);
					if(!vectorDataValues.contains("VectorValue")){
						vectorDataValues.add("VectorValue");
					}
				}else{
					point.putValue(labelTextField.getText(),v1);
					if(!vectorDataValues.contains(labelTextField.getText())){
						vectorDataValues.add(labelTextField.getText());
					}
				}
				
			}
		}
		this.vectorValuesList.setListData(vectorDataValues);
		this.postpModel = postp;
		this.intFrame.setPostpModel(postp);
		this.intFrame.setStep(postp.getStepList().get(0));
	}
	
	public void view(ActionEvent event){
		this.state.setDeformedOn(true);
		if(this.openInANewWindowCheckBox.isSelected()){
			InterfacePostp intFrame = new InterfacePostp();
			intFrame.validate();
			intFrame.setPostpModel(this.postpModel);
			intFrame.setModel(this.postpModel.getStepList().get(0));
			intFrame.setTitle(this.postpModel.getLabel());
			((DesktopPostp)this.desktop).addInternalFrame(intFrame);
			intFrame.toFront();
			this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
			this.state.getEdgeViewState().setLineColor(Color.WHITE);
			this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setElementsContoursComputed(true);
			this.state.getElementsPlotContoursViewState().setKeyValue((String)this.vectorValuesList.getSelectedValue());
			if(onGaussCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onFullMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(true);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(true);
			}
			this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
			((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
			(intFrame).getView().getDrawingArea().repaint();    
		}else{
			this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
			this.state.getEdgeViewState().setLineColor(Color.WHITE);
			this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setElementsContoursComputed(true);
			this.state.getElementsPlotContoursViewState().setKeyValue((String)this.vectorValuesList.getSelectedValue());
			if(onGaussCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onFullMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(true);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(true);
			}
			this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
			((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
			((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
		}
	}
	
	private class ChoiceListenerOnGaussMesh implements ActionListener{
		public void actionPerformed(ActionEvent event){
			onGaussCheckBox.setSelected(true);
			onFullMeshCheckBox.setSelected(false);
			onMeshCheckBox.setSelected(false);
		}    
	}
	
	private class ChoiceListenerOnFullMesh implements ActionListener{
		public void actionPerformed(ActionEvent event){
			onGaussCheckBox.setSelected(false);
			onFullMeshCheckBox.setSelected(true);
			onMeshCheckBox.setSelected(false);
		}    
	}
	
	private class ChoiceListenerOnMesh implements ActionListener{
		public void actionPerformed(ActionEvent event){
			onGaussCheckBox.setSelected(false);
			onFullMeshCheckBox.setSelected(false);
			onMeshCheckBox.setSelected(true);
		}    
	}
	
	private String addText(JList list){
		String text="";
		for(int i=0;i<list.getSelectedValues().length;i++){
			text+=((String)list.getSelectedValues()[i]);
		}
		return text;
	}
	
//	************************************************************************************************************   
}
